
package com.jf.cloud.combo.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import com.jf.cloud.api.order.feign.OrderFeignClient;
import com.jf.cloud.combo.service.ComboOrderService;
import com.jf.cloud.combo.service.ComboService;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.order.constant.OrderActivityType;
import com.jf.cloud.common.order.vo.OrderActivityVO;
import com.jf.cloud.common.product.dto.ComboDTO;
import com.jf.cloud.common.response.ServerResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 套餐订单表
 *
 * @author zz
 * @date 2022-08-16 09:18:06
 */
@Service
public class ComboOrderServiceImpl implements ComboOrderService {

    @Autowired
    private ComboService comboService;
    @Autowired
    private OrderFeignClient orderFeignClient;

//    @Override
//    public PageVO<ComboOrder> page(PageDTO pageDTO) {
//        return PageUtil.doPage(pageDTO, () -> comboOrderMapper.list());
//    }
//
//    @Override
//    public ComboOrder getByComboOrderId(Long comboOrderId) {
//        return comboOrderMapper.getByComboOrderId(comboOrderId);
//    }
//
//    @Override
//    public void save(ComboOrder comboOrder) {
//        comboOrderMapper.save(comboOrder);
//    }
//
//    @Override
//    public void update(ComboOrder comboOrder) {
//        comboOrderMapper.update(comboOrder);
//    }
//
//    @Override
//    public void deleteById(Long comboOrderId) {
//        comboOrderMapper.deleteById(comboOrderId);
//    }
//
//    @Override
//    public void batchSave(List<ComboOrderDTO> comboOrderList) {
//        comboOrderMapper.batchSave(comboOrderList);
//    }
//
//    @Override
//    public void saveComboOrders(ShopCartOrderMergerVO mergerOrder) {
//        Long userId = mergerOrder.getUserId();
//        //保存套餐订单记录
//        List<ComboOrderDTO> comboOrderDTOList = new ArrayList<>();
//        List<ShopCartOrderVO> cartOrders = mergerOrder.getShopCartOrders();
//        Map<Long, List<ShopCartOrderVO>> orderMap = cartOrders.stream().collect(Collectors.groupingBy(ShopCartOrderVO::getOrderId));
//        List<ShopCartVO> shopCartVOList = new ArrayList<>();
//        for (ShopCartOrderVO cartOrder : cartOrders) {
//            ShopCartOrderVO vo = new ShopCartOrderVO();
//            vo = cartOrder;
//            List<ShopCartItemDiscountVO> discounts = cartOrder.getShopCartItemDiscounts();
//            vo.setShopCartItemDiscounts(null);
//            ShopCartVO shopCartVO = mapperFacade.map(vo, ShopCartVO.class);
//            shopCartVO.setShopCartItemDiscounts(discounts);
//            cartOrder.setShopCartItemDiscounts(discounts);
//            shopCartVOList.add(shopCartVO);
//        }
//
//        //获取被拆单的套餐虚拟商品 key--套餐主商品购物车id  value--虚拟商品价格
//        Map<Long,Long> moldSpuPriceMap = new HashMap<>(16);
//        for (ShopCartVO shopCartVO : shopCartVOList) {
//            for (ShopCartItemDiscountVO shopCartItemDiscount : shopCartVO.getShopCartItemDiscounts()) {
//                for (ShopCartItemVO shopCartItem : shopCartItemDiscount.getShopCartItems()) {
//                    if (Objects.nonNull(shopCartItem.getSpuMold()) && Objects.equals(shopCartItem.getSpuMold(),1) && Objects.nonNull(shopCartItem.getComboId())){
//                        moldSpuPriceMap.put(shopCartItem.getParentCartItemId(),shopCartItem.getMatchingPrice());
//                    }
//                }
//            }
//        }
//
//        List<ShopCartVO> shopCartVOS = discountFeignClient.calculateDiscountAndMakeUpShopCart(shopCartVOList).getData();
//        for (ShopCartVO shopCartVO : shopCartVOS) {
//            List<ShopCartItemDiscountVO> shopCartItemDiscounts = shopCartVO.getShopCartItemDiscounts();
//            for (ShopCartItemDiscountVO shopCartItemDiscount : shopCartItemDiscounts) {
//                if (Objects.isNull(shopCartItemDiscount.getType())) {
//                    continue;
//                }
//                ChooseComboItemVo chooseComboItemVo = shopCartItemDiscount.getChooseComboItemVo();
//                List<ShopCartItemVO> shopCartItems = shopCartItemDiscount.getShopCartItems();
//                //套餐主商品id
//                Long comboMainSpuId = 0L;
//                Long orderId = 0L;
//                Long payPrice = 0L;
//                for (ShopCartItemVO shopCartItem : shopCartItems) {
//                    payPrice += shopCartItem.getMatchingPrice();
//                    if (Objects.nonNull(shopCartItem.getComboId()) && Objects.isNull(shopCartItem.getParentCartItemId())){
//                        comboMainSpuId = shopCartItem.getSpuId();
//                        //获取被拆单的虚拟商品价格
//                        Long moldPrice = moldSpuPriceMap.get(shopCartItem.getCartItemId());
//                        if (Objects.nonNull(moldPrice)){
//                            payPrice += moldPrice;
//                        }
//                    }
//                }
//                //获取主商品所在的订单id
//                for (List<ShopCartOrderVO> value : orderMap.values()) {
//                    for (ShopCartOrderVO shopCartOrderVO : value) {
//                        List<ShopCartItemDiscountVO> shopCartItemDiscountVOList = shopCartOrderVO.getShopCartItemDiscounts();
//                        for (ShopCartItemDiscountVO discountVO : shopCartItemDiscountVOList) {
//                            List<ShopCartItemVO> shopCartItemVOList = discountVO.getShopCartItems();
//                            List<Long> spuIdList = shopCartItemVOList.stream().map(e -> e.getSpuId()).collect(Collectors.toList());
//                            if (spuIdList.contains(comboMainSpuId)){
//                                orderId = shopCartOrderVO.getOrderId();
//                            }
//                        }
//                    }
//                }
//                //保存套餐订单信息
//                ComboOrderDTO comboOrderDTO = new ComboOrderDTO();
//                comboOrderDTO.setShopId(shopCartVO.getShopId());
//                comboOrderDTO.setComboId(chooseComboItemVo.getComboId());
//                comboOrderDTO.setUserId(userId);
//                comboOrderDTO.setComboNum(chooseComboItemVo.getComboCount());
//                comboOrderDTO.setPayPrice(payPrice);
//                comboOrderDTO.setOrderNumber(orderId);
//                comboOrderDTO.setStatus(PayStatus.UNPAY.value());
//                comboOrderDTOList.add(comboOrderDTO);
//            }
//        }
//        if (comboOrderDTOList.size() == 0){
//            return;
//        }
//        batchSave(comboOrderDTOList);
//    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void comboOrderPaySucceed(List<Long> orderIds) {
        //套餐订单变为已支付
        ServerResponseEntity<List<OrderActivityVO>> serverResponseEntity = orderFeignClient.getActivitySalesQuantity(orderIds, OrderActivityType.COMBO.value());
        if (serverResponseEntity.isFail()) {
            throw new LuckException(serverResponseEntity.getMsg());
        }
        if (CollUtil.isEmpty(serverResponseEntity.getData())) {
            return;
        }
        //套餐销量增加
        Map<Long, ComboDTO> map = new HashMap<>();
        for (OrderActivityVO orderActivityVO : serverResponseEntity.getData()) {
            ComboDTO combo = new ComboDTO();
            combo.setComboId(orderActivityVO.getActivityId());
            combo.setSoldNum(orderActivityVO.getCount());
            map.put(orderActivityVO.getActivityId(), combo);
        }

        if (MapUtil.isNotEmpty(map)){
            comboService.comboSoldNumAdd(new ArrayList<>(map.values()));
        }
    }
}
